<?
// Remote Procedure Call dispatcher
//header('Content-type: text/x-json');
header('Content-type: text/plain');

require("classes/RPCStuff.php");
require("classes/Helpers.php");

// remove the magic quotes, because magic quotes f!cking suck
if( get_magic_quotes_gpc () ) {
    if (!empty($_GET))     Helpers::remove_magic_quotes( $_GET );
    if (!empty($_POST))    Helpers::remove_magic_quotes( $_POST );
    if (!empty($_COOKIE))  Helpers::remove_magic_quotes( $_COOKIE );
    //if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
}

$json_reqstring = $_POST['json'];

// Override the POST args (mostly for debugging)
if($_GET['json'])
    $json_reqstring = $_GET['json'];

if(!$json_reqstring) {
    echo json_encode(new RPC_Error( "unknown", "JSON_STRING", "Missing Json string"));
    return;
}


// hack for broken json encoding in mootools
//$json_reqstring = preg_replace( '/\n/', '\n', $json_reqstring );

//error_log( "Reqstring:" . $json_reqstring);
$json_request = json_decode( $json_reqstring );
if(!$json_request) {
    // $fhandle = fopen('/tmp/jason', 'w');
    // fwrite( $fhandle, $json_reqstring );
    // fclose( $fhandle );

    echo json_encode(new RPC_Error( "unknown", "JSON_PARSE", "Unparsable Json string"));
    return;
}

$operation = $json_request->operation;
if(!$operation) {
    echo json_encode(new RPC_Error( $operation, "OPERATION", "Missing operation param"));
    return;
}

//error_log( "Answering operation $operation" );

// Initialize DB connection and choose a method
require_once ("classes/init.php");


$json_response = null;

// Check if the service is closed
$service_avail = $PARAPARA->config->get('GLOBAL', 'ON');
if($service_avail != 'true') {
    $json_response = new RPC_Error( $operation, "UNAVAILABLE", "The service is unavailable");
}

else {
    if($operation == 'get_bit') {
        $json_response = include "remote/get_bit.php";        
    }

    // Chat RPCs ////////////////////////////////////////
    elseif($operation == 'chat_msglist') {
        $json_response = include "remote/chat_msglist.php";
    }
    elseif($operation == 'chat_post_msg') {
        $json_response = include "remote/chat_post_msg.php";
    }
    elseif($operation == 'chat_del_msgs') {
        $json_response = include "remote/chat_del_msgs.php";
    }

    // Forum RPCS ////////////////////////////////////////
    elseif($operation == 'forum_get_threads') {
        $json_response = include "remote/forum_get_threads.php";
    }
    elseif($operation == 'forum_get_thread') {
        $json_response = include "remote/forum_get_thread.php";
    }
    elseif($operation == 'forum_get_body') {
        $json_response = include "remote/forum_get_body.php";
    }
    elseif($operation == 'forum_create_thread') {
        $json_response = include "remote/forum_create_thread.php";
    }
    elseif($operation == 'forum_post_reply') {
        $json_response = include "remote/forum_post_reply.php";
    }
    elseif($operation == 'forum_preview_msg') {
        $json_response = include "remote/forum_preview_msg.php";
    }

    // Linkroll RPCS /////////////////////////////////////
    elseif($operation == 'linkroll_post_link') {
        $json_response = include "remote/linkroll_post_link.php";
    }
    elseif($operation == 'linkroll_longlist') {
        $json_response = include "remote/linkroll_longlist.php";
    }


    // Token RPCS ////////////////////////////////////////
    elseif($operation == 'get_token') {
        $json_response = include "remote/get_token.php";
    }
    elseif($operation == 'check_token') {
        $json_response = include "remote/check_token.php";
    }
    elseif($operation == 'get_events') {
        $json_response = include "remote/get_events.php";
    }
    elseif($operation == 'para_info') {
        $json_response = include "remote/para_info.php";
    }
    else {
       $json_response = new RPC_Error( $operation, "OPERATION", "Unknown operation");

    }
}

$json_response->postback = $json_request->postback;
if($PARAPARA->token_validated )
    $json_response->token = $PARAPARA->make_new_token();


echo json_encode( $json_response );

return;

?>